﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AnTouch.UI.WinForms.Controls
{
    public partial class UCTraceBar : UserControl
    {
        public UCTraceBar()
        {
            InitializeComponent();
            trackBar_MouseCaptureChanged(null, null);
        }

        private EventHandler _valueChangeEvent;

        [Description("滚动改变事件"), Category("自定义")]
        public EventHandler ValueChangeEvent
        {
            get
            {
                return _valueChangeEvent;
            }
            set
            {
                _valueChangeEvent += value;
            }
        }

        private double _minValue = 1;
        /// <summary>
        /// 滑块的最小值
        /// </summary>
        public double MinValue
        {
            get
            {
                return _minValue;
            }
            set
            {
                _minValue = value;
                label1.Text = value.ToString("f4");
            }
        }

        private double _maxValue = 10;
        /// <summary>
        /// 滑块的最大值
        /// </summary>
        public double MaxValue
        {
            get
            {
                return _maxValue;
            }
            set
            {
                _maxValue = value;
            }
        }

        private double _labelText = 0;
        public double LabelText
        {
            get
            {
                return _labelText;
            }
            set
            {
                _labelText = value;
                label1.Text = value.ToString("f4");
            }
        }

        /// <summary>
        /// 值改变时触发函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trackBar_MouseCaptureChanged(object sender, EventArgs e)
        {
            LabelText = (_maxValue - _minValue) / 10 * (trackBar1.Value - 1) + _minValue;
            if (trackBar1.Value == 1)
                LabelText = _minValue;
            if (trackBar1.Value == 10)
                LabelText = _maxValue;
        }

        private void trackBar_MouseDown(object sender, MouseEventArgs e)
        {
            trackBar_MouseCaptureChanged(sender, e);
        }

        private void trackBar_MouseHover(object sender, EventArgs e)
        {
            trackBar_MouseCaptureChanged(sender, e);
        }

        private void trackBar_Scroll(object sender, EventArgs e)
        {
            trackBar_MouseCaptureChanged(sender, e);
        }

        private void trackBar_ValueChanged(object sender, EventArgs e)
        {
            if (ValueChangeEvent != null)
            {
                _valueChangeEvent(sender, e);
            }
        }
    }
}


